//
//  HttpRequest.m
//  IWaneHome
//
//  Created by hwd on 印度历1938/4/7.
//  Copyright © 印度历1938年 hwd_zlg. All rights reserved.
//

#import "HttpRequest.h"
#import "AFNetworking.h"
@interface HttpRequest ()
@end
@implementation HttpRequest

//get通用接口Dic
+(void)GetrequestUrl:(NSString *)url Dic:(NSDictionary *)dic completion:(void (^)(NSDictionary *responseDic))complete
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:[NSString stringWithFormat:@"%@%@",baseUrl,url] parameters:dic progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 请求成功，解析数据
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        if (complete) {
            complete(jsonDic);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        // 请求失败
        NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:@"0",@"success", nil];
        if (complete) {
            complete(dic);
        }
    }];
}

//post通用接口
+(void)PostrequestUrl:(NSString *)url Dic:(NSDictionary *)dic completion:(void (^)(NSDictionary *responseDic))complete
{
//    NSString *urlStringUTF8 = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    [manager POST:[NSString stringWithFormat:@"%@%@",baseUrl,url] parameters:dic constructingBodyWithBlock:^(id  _Nonnull formData) {
    } progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
     // 请求成功
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        if (complete) {//完成
            complete(jsonDic);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        // 请求失败
        NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:@"0",@"success", nil];
        if (complete) {//完成
            complete(dic);
        }
    }];
}
//post通用接口2 
+(void)PostMuTableDicrequestUrl:(NSString *)url Dic:(NSMutableDictionary *)dic Controller:(UIViewController *)selfVC completion:(void (^)(NSDictionary *responseDic))complete
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:[NSString stringWithFormat:@"%@%@",baseUrl,url] parameters:dic constructingBodyWithBlock:^(id  _Nonnull formData) {
    } progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 请求成功，解析数据
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        if (complete) {//完成
            complete(jsonDic);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        // 请求失败
        NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:@"0",@"success", nil];
        if (complete) {//完成
            complete(dic);
        }
    }];
}

+ (NSDictionary *)dictionaryWithJsonString:(NSData *)data {
    if (data == nil) {//判空
        return nil;
    }
    //替换特殊符号
    NSString *responseString = [[NSString alloc] initWithData:data  encoding:NSUTF8StringEncoding];
    responseString = [responseString stringByReplacingOccurrencesOfString:@"\r\n" withString:@""];
    responseString = [responseString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    responseString = [responseString stringByReplacingOccurrencesOfString:@"\t" withString:@""];
    NSData *jsonData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err) {//错误
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
    
}
@end
